//
//  AVDSettingVC.m
//  AVD_Demo
//
//  Created by 3tee on 2022/11/16.
//

#import "AVDSettingVC.h"

@interface AVDSettingVC ()
@property (weak, nonatomic) IBOutlet UITextField *serverTf;
@property (weak, nonatomic) IBOutlet UISwitch *audioSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *videoSwitch;

@end

@implementation AVDSettingVC{
    BOOL _micOn;
    BOOL _camOn;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupUI];
}

- (void)setupUI{
    self.navigationItem.title = @"设置";
    
    _micOn = [[NSUserDefaults standardUserDefaults] boolForKey:micKey];
    _camOn = [[NSUserDefaults standardUserDefaults] boolForKey:camKey];
    [self.audioSwitch setOn:_micOn animated:YES];
    [self.videoSwitch setOn:_camOn animated:YES];
    if([[NSUserDefaults standardUserDefaults] valueForKey:serverKey]){
        self.serverTf.text = [[NSUserDefaults standardUserDefaults] valueForKey:serverKey];
    }else{
        self.serverTf.text = serverUrl;
    }

}


- (IBAction)audioClicked:(UISwitch *)sender {
    sender.selected = !sender.selected;
//    _micOn = sender.selected;
    _micOn = !_micOn;
}


- (IBAction)videoClicked:(UISwitch *)sender {
    sender.selected = !sender.selected;
//    _camOn = sender.selected;
    _camOn = !_camOn;
}


- (IBAction)saveClicked:(id)sender {
    [[NSUserDefaults standardUserDefaults] setBool:_micOn forKey:micKey];
    [[NSUserDefaults standardUserDefaults] setBool:_camOn forKey:camKey];
    [[NSUserDefaults standardUserDefaults] setValue:_serverTf.text forKey:serverKey];
    if(self.reInitBlock){
        self.reInitBlock(_serverTf.text);
    }
    [[NSUserDefaults standardUserDefaults] synchronize];
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}


@end
